<template>
  <div>
    <div id="nav">
      <router-link to="/home" exact-path>Home</router-link>
      |
      <router-link to="/news" exact-path>News</router-link>
      |
      <div class="user-name">
        <span v-if="status === 'loading'">loading...</span>

        <template v-else-if="status === 'login'">
          <router-link to="/user" exact-path>{{ user.name }}</router-link>
          <a href="" @click.prevent="handleLoginOut">退出</a>
        </template>
        <router-link v-else to="/login" exact-path>Login</router-link>
      </div>
    </div>
    <router-view />
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
  computed: {
    ...mapGetters("loginUser", ["status"]),
    ...mapState("loginUser", ["user"]),
  },
  methods: {
    async handleLoginOut() {
      await this.$store.dispatch("loginUser/loginOut");
      this.$router.push("/login");
    },
  },
};
</script>

<style>
body {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
#nav {
  padding: 30px;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color: #42b983;
}
#nav .ml-5 {
  margin-left: 5px;
}

.user-name {
  display: inline-block;
}
.user-name a,
.user-name span {
  margin-right: 15px;
}
</style>
